package com.kws.tool.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.tool.dto.CreateNormManagementRq;
import com.kws.tool.dto.QueryNormManagementRq;
import com.kws.tool.dto.UpdateNormManagementRq;
import com.kws.tool.entity.TbNormManagement;
import com.kws.tool.mapper.NormManagementMapper;
import com.kws.tool.service.NormManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <b>NormManagementServiceImpl</b>
 *
 * @description: NormManagementServiceImpl <br>
 * @date: 2024/11/20 19:19 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class NormManagementServiceImpl extends ServiceImpl<NormManagementMapper, TbNormManagement> implements NormManagementService {
    private static final Logger logger = LoggerFactory.getLogger(NormManagementServiceImpl.class);

    @Override
    public Page<TbNormManagement> queryNormManagementPage(QueryNormManagementRq rq) {
        Page<TbNormManagement> page = new Page<>(rq.getPageNum(),rq.getPageSize());
        Page<TbNormManagement> pageInfo = baseMapper.queryNormManagementPage(page,rq);
        return pageInfo;
    }

    @Override
    public boolean createNormManagement(CreateNormManagementRq rq) {
        TbNormManagement tbNormManagement = new TbNormManagement();
        BeanUtil.copyProperties(rq,tbNormManagement);
        tbNormManagement.setPageView(0);
        Ksys.setBasicModelData(tbNormManagement, SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(tbNormManagement);
        if (1 == insert){
            return true;
        }
        return false;
    }

    @Override
    public TbNormManagement queryNormManagementById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean updateNormManagement(UpdateNormManagementRq rq) {
        TbNormManagement tbNormManagement = baseMapper.selectById(rq.getId());
        BeanUtil.copyProperties(rq,tbNormManagement);
        Ksys.setBasicModelData(tbNormManagement,SecurityUtils.getStringUserId(),KConstants.DATA_ENABLE,ModelOpType.UPDATE);
        int i = baseMapper.updateById(tbNormManagement);
        if (1 == i){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteNormManagementById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
